#!/bin/sh
### BEGIN INIT INFO
# Provides: kiwid
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Required-Start:
# Required-Stop:
# Short-Description: run kiwi daemon
# Description: run kiwi daemon
### END INIT INFO

# set false when controlling SPI hardware directly (PIO mode):
USE_SPIDEV=false

KIWID=kiwid
KIWID_EXEC=/usr/local/bin/$KIWID
KIWID_ARGS="-bg"

[ -x $KIWID_EXEC ] || exit 1

RPI=$(cat /proc/device-tree/model | grep -q -s "Raspberry Pi 3" && echo true)
BBAI=$(cat /proc/device-tree/model | grep -q -s "BeagleBone AI" && echo true)
if [ "x${BBAI}" = "xtrue" ] ; then
	echo "BBAI USE_SPIDEV"
	DEBIAN=9
	USE_SPIDEV=1
elif [ "x${RPI}" = "xtrue" ] ; then
	echo "RPI USE_SPIDEV"
	DEBIAN=10
	USE_SPIDEV=1
else

# Debian 7, PIO: load our cape-bone-S overlay via echo > slots
# Debian 7, SPIDEV: BB-SPIDEV0 loaded via echo > slots
# Debian 8, PIO: load our cape-bone-S overlay via echo > slots
# Debian 8, SPIDEV: Bug: BB-SPIDEV0 must be loaded via /boot/uEnv.txt to config pmux properly

USE_SPIDEV=1

SLOTS="/sys/devices/platform/bone_capemgr/slots"
if test ! -f ${SLOTS} ; then
	echo DEBIAN 7
	DEBIAN=7
	# do not use USE_SPIDEV on Debian 7
	USE_SPIDEV=0
	LOAD_SPIDEV=yes
	LOAD_SPIPIO=yes
	SLOTS=`ls /sys/devices/bone_capemgr.*/slots`
else
	echo DEBIAN 8
	DEBIAN=8
	LOAD_SPIDEV=no
	LOAD_SPIPIO=no
fi

DEV=kiwi
DEVID=cape-bone-${DEV}

if [ "x${USE_SPIDEV}" = "x1" ] ; then
	# use SPIDEV driver (with built-in DMA) for SPI:
	echo USE_SPIDEV
	DEV_SPI=SPIDEV0
	DEVID_SPI=BB-${DEV_SPI}
	LOAD_SPI=${LOAD_SPIDEV}
else
	echo USE_SPIPIO
	DEV_SPI=${DEV}-S
	DEVID_SPI=cape-bone-${DEV_SPI}
	LOAD_SPI=${LOAD_SPIPIO}
fi

echo LOAD_SPI = ${LOAD_SPI}

DEV_PRU=${DEV}-P
DEVID_PRU=cape-bone-${DEV_PRU}

CAPE=${DEVID}-00A0
SPI=${DEVID_SPI}-00A0
PRU=${DEVID_PRU}-00A0

fi

case "$1" in

  start)
    echo "Starting kiwid"

# cape
    if [ "x${BBAI}" = "xtrue" ] ; then
        sleep 1
    elif [ "x${RPI}" = "xtrue" ] ; then
        modprobe i2c-dev
        modprobe at24
	cat "1200000" > /sys/devices/system/cpu/cpufreq/policy0/scaling_min_freq
        # Original version of kiwisdr
        echo "24c32 0x54" > /sys/class/i2c-adapter/i2c-1/new_device
        # RaspSDR version
        echo "24c32 0x50" > /sys/class/i2c-adapter/i2c-1/new_device
        sleep 1
        if test -e "/sys/bus/i2c/devices/1-0054/eeprom"; then
            echo "KiwiSDR Detected"
	else
            echo "RaspSDR Detected"
	    KIWID_ARGS="${KIWID_ARGS} -raspsdr"
	    # start fan
	    gpio mode 26 pwm
	    gpio pwm 26 1024
	fi
    else
        if test \( ! -f /lib/firmware/${CAPE}.dtbo \) -o \( /lib/firmware/${CAPE}.dts -nt /lib/firmware/${CAPE}.dtbo \) ; then
            echo compile ${DEV} device tree;
            dtc -O dtb -o /lib/firmware/${CAPE}.dtbo -b 0 -@ /lib/firmware/${CAPE}.dts;
            # don't unload old slot because this is known to cause panics; must reboot
        fi
    
        if ! grep -q ${DEVID} ${SLOTS} ; then
            echo load ${DEV} device tree;
            echo ${DEVID} > ${SLOTS};
            sleep 2;
        fi

# SPI
        if test \( -f /lib/firmware/${SPI}.dts \) -a \( \( ! -f /lib/firmware/${SPI}.dtbo \) -o \( /lib/firmware/${SPI}.dts -nt /lib/firmware/${SPI}.dtbo \) \) ; then
            echo compile ${DEV_SPI} device tree;
            dtc -O dtb -o /lib/firmware/${SPI}.dtbo -b 0 -@ /lib/firmware/${SPI}.dts;
            # don't unload old slot because this is known to cause panics; must reboot
        fi
    
        if [ "x${LOAD_SPI}" = "xyes" ] ; then
            if ! grep -q ${DEVID_SPI} ${SLOTS} ; then
                echo load ${DEV_SPI} device tree;
                echo ${DEVID_SPI} > ${SLOTS};
                sleep 2;
            fi
        fi

# PRU (future)
        if test \( ! -f /lib/firmware/${PRU}.dtbo \) -o \( /lib/firmware/${PRU}.dts -nt /lib/firmware/${PRU}.dtbo \) ; then
            echo compile ${DEV_PRU} device tree;
            dtc -O dtb -o /lib/firmware/${PRU}.dtbo -b 0 -@ /lib/firmware/${PRU}.dts;
            # don't unload old slot because this is known to cause panics; must reboot
        fi
        
        if ! grep -q ${DEVID_PRU} ${SLOTS} ; then
            echo load ${DEV_PRU} device tree;
            echo ${DEVID_PRU} > ${SLOTS};
        fi
    fi

	echo -n "Start kiwid: "
	start-stop-daemon --start --background --exec $KIWID_EXEC -- "-debian" ${DEBIAN} "-use_spidev" ${USE_SPIDEV} $KIWID_ARGS
	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
		echo "OK"
	else
		echo "FAIL"
	fi

    # have to do something here that returns zero status, otherwise systemd immediately stops us. Why?
    date
    ;;

  stop)
	echo -n "Stopping kiwid: "
	start-stop-daemon --stop --name $KIWID
	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
		echo "OK"
	else
		echo "FAIL"
	fi
    ;;

  status)
    if [ -n "`/bin/pidof $KIWID`" ] ; then
        echo "kiwid is running."
    else
        echo "kiwid is not running."
    fi
    ;;

  restart)
    $0 stop && sleep 1 && $0 start
    ;;

  *)
    echo "Usage: /etc/init.d/kiwid {start|stop|status|restart}"
    exit 1

esac

exit 0
